﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TextMIS.体检管理
{
    public partial class frmPackage : Form
    {
        public frmPackage()
        {
            InitializeComponent();
        }

        private void frmPackage_Load(object sender, EventArgs e)
        {
            //new Common.Common().AddImages(imageList1);
            Tr_Paglist.ImageList = imageList1;

            lv_Itemlist.SmallImageList = imageList1;
            lv_Itemlist.StateImageList = imageList1;
            lv_Itemlist.LargeImageList = imageList1;

            DataBind();
        }
        sysopr xtbiz = new sysopr();
        dbopr ywszbiz = new dbopr();
        string str_bh = "";//套餐编号
        DataTable dt_tj_tc_dc = null;
        void DataBind()
        {
            Tr_Paglist.Nodes.Clear();

            TreeNode node = new TreeNode("体检套餐");
            DataTable dt_tjtc = ywszbiz.Get_tj_tc_hd();
            foreach (DataRow dr in dt_tjtc.Rows)
            {
                string str_bh = dr["bh"].ToString().Trim();//类型编号（科室）
                TreeNode node1 = new TreeNode(dr["mc"].ToString().Trim());
                node1.Tag = str_bh;

                node.Nodes.Add(node1);
            }
            Tr_Paglist.Nodes.Add(node);
            Tr_Paglist.ExpandAll();
            //new Common.Common().AddImage(node);

            cb_sex.DataSource = xtbiz.GetSex(1);//性别
            cb_sex.DisplayMember = "xmmc";
            cb_sex.ValueMember = "bzdm";

            cb_style.DataSource = xtbiz.GetSex(10);//业务类型
            cb_style.DisplayMember = "xmmc";
            cb_style.ValueMember = "bzdm";
        }


        private void tv_tc_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (object.Equals(null, Tr_Paglist.SelectedNode.Tag)) return;

            str_bh = Tr_Paglist.SelectedNode.Tag.ToString().Trim();
            this.Text = "体检套餐设置【" + Tr_Paglist.SelectedNode.Text.Trim() + "】";
            DataTable dt_tj_tc_hd = ywszbiz.Get_tj_tc_hd(str_bh);
            if (dt_tj_tc_hd.Rows.Count < 1) return;
            tb_PagId.Text = dt_tj_tc_hd.Rows[0]["bh"].ToString().Trim();
            tb_disp_order.Text = dt_tj_tc_hd.Rows[0]["disp_order"].ToString().Trim();
            cb_sex.SelectedValue = dt_tj_tc_hd.Rows[0]["xb"].ToString().Trim();
            tb_name.Text = dt_tj_tc_hd.Rows[0]["mc"].ToString().Trim();
            tb_sname.Text = dt_tj_tc_hd.Rows[0]["jc"].ToString().Trim();
            tb_price.Text = dt_tj_tc_hd.Rows[0]["bzjg"].ToString().Trim();
            tb_sale.Text = dt_tj_tc_hd.Rows[0]["jg"].ToString().Trim();
            try
            {
                tb_rebate.Text = (Convert.ToDecimal(tb_sale.Text) / Convert.ToDecimal(tb_price.Text) * 100).ToString("0.00");
            }
            catch
            {

            }
            cb_style.SelectedValue = dt_tj_tc_hd.Rows[0]["tjywlx"].ToString().Trim();
            tb_memo.Text = dt_tj_tc_hd.Rows[0]["bz"].ToString().Trim();

            lv_Itemlist.Items.Clear();
            lv_Itemlist.View = View.Tile;
            dt_tj_tc_dc = ywszbiz.Get_tj_tc_dt(str_bh);
            foreach (DataRow dr in dt_tj_tc_dc.Rows)
            {
                ListViewItem item = new ListViewItem();
                item.Tag = dr["zhxm"].ToString().Trim();
                item.Text = dr["mc"].ToString().Trim();
                item.ImageIndex = 4;
                lv_Itemlist.Items.Add(item);
            }
            //lv_tjtcxm.SmallImageList = imageList1;
            //lv_tjtcxm.StateImageList = imageList1;
            //lv_tjtcxm.LargeImageList = imageList1;

            if (Tr_Paglist.SelectedNode.Nodes.Count == 0)
            {
                Tr_Paglist.SelectedNode.SelectedImageIndex = Tr_Paglist.SelectedNode.ImageIndex;
            }
        }

        private void txt_dz_TextChanged(object sender, EventArgs e)
        {
            try
            {
                tb_sale.Text = Convert.ToString(Convert.ToDecimal(tb_price.Text) * Convert.ToDecimal(tb_rebate.Text) / 100);
            }
            catch
            {

            }
        }

        private void 清除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            lv_Itemlist.Items.Clear();
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                lv_Itemlist.Items.Remove(lv_Itemlist.SelectedItems[0]);
                Get_Bzjg(lv_Itemlist);
            }
            catch
            {

            }
        }

        private void bt_delete_Click(object sender, EventArgs e)
        {
            if (str_bh == "") return;
            if (DialogResult.Yes == MessageBox.Show("是否真的删除？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1))
            {
                //dbopr ywszbiz1 = new dbopr();
                ywszbiz.str_Delete_tj_tc_hd(str_bh);
                ywszbiz.str_Delete_tj_tc_dc(str_bh);
                ywszbiz.Exec_ArryList();
                MessageBox.Show("删除成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Tr_Paglist.Nodes.Remove(Tr_Paglist.SelectedNode);
                ClearControl();
            }
        }

        private void bt_exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        void ClearControl()
        {
            tb_PagId.Text = "";
            tb_disp_order.Text = "";
            cb_sex.SelectedIndex = -1;
            tb_name.Text = "";
            tb_sname.Text = "";
            tb_price.Text = "0.00";
            tb_rebate.Text = "100";
            tb_sale.Text = "0.00";
            cb_style.SelectedIndex = 0;
            tb_memo.Text = "";
            lv_Itemlist.Items.Clear();
            str_bh = "";
        }
        private void bt_save_Click(object sender, EventArgs e)
        {
            if (object.Equals(null, cb_sex.SelectedValue))
            {
                MessageBox.Show("请选择性别！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.ActiveControl = cb_sex;
                return;
            }
            if (tb_name.Text.Trim() == "")
            {
                MessageBox.Show("请填写套餐名称！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.ActiveControl = tb_name;
                return;
            }
            if (object.Equals(null, cb_style.SelectedValue))
            {
                MessageBox.Show("请选择业务类型！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.ActiveControl = cb_style;
                return;
            }

            if (lv_Itemlist.Items.Count <= 0)
            {
                MessageBox.Show("体检套餐项目不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //this.ActiveControl = cmb_tjywlx;
                return;
            }

            if (tb_PagId.Text.Trim() == "")
            {
                str_bh = xtbiz.GetHmz("tcid", 1);
                tb_PagId.Text = str_bh;
            }
            //ywszBiz ywszbiz1 = new ywszBiz();
            ywszbiz.str_Insert_tj_tc_hd(tb_PagId.Text.Trim(), tb_disp_order.Text.Trim(), cb_sex.SelectedValue.ToString().Trim(), tb_name.Text.Trim(),
                tb_sname.Text.Trim(), tb_price.Text.Trim(), tb_sale.Text.Trim(), cb_style.SelectedValue.ToString().Trim(), tb_memo.Text.Trim());
            ywszbiz.str_Delete_tj_tc_dc(str_bh);
            foreach (ListViewItem item in lv_Itemlist.Items)
            {
                ywszbiz.str_Insert_tj_tc_dc(str_bh, item.Tag.ToString().Trim());
            }
            ywszbiz.Exec_ArryList();
            MessageBox.Show("保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            DataBind();
        }

        private void bt_add_Click(object sender, EventArgs e)
        {
            if (str_bh == "") return;
            ClearControl();
            dt_tj_tc_dc.Rows.Clear();
        }

        private void 增加ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (object.Equals(dt_tj_tc_dc, null))
            {
                dt_tj_tc_dc = ywszbiz.Get_tj_tc_dt("0");
            }
            if (cb_sex.SelectedValue == null)
            {
                return;
            }
            //Form_zhxmlr frm = new Form_zhxmlr(dt_tj_tc_dc, cb_sex.SelectedValue.ToString().Trim());
            //if (frm.ShowDialog() == DialogResult.OK)
            //{
            //    lv_Itemlist.Items.Clear();
            //    lv_Itemlist.View = View.Tile;
            //    dt_tj_tc_dc = frm.dt_tj_tc_dt;
            //    foreach (DataRow dr in dt_tj_tc_dc.Rows)
            //    {
            //        ListViewItem item = new ListViewItem();
            //        item.Tag = dr["zhxm"].ToString().Trim();
            //        item.Text = dr["mc"].ToString().Trim();
            //        item.ImageIndex = 4;
            //        lv_Itemlist.Items.Add(item);
            //    }
            //    Get_Bzjg(lv_Itemlist);
            //}
        }
        private void Get_Bzjg(ListView lv)//获取标准价格
        {
            string str_sql = "(";
            foreach (ListViewItem item in lv.Items)
            {
                str_sql = str_sql + "'" + item.Tag.ToString().Trim() + "',";
            }
            str_sql = str_sql.Substring(0, str_sql.Length - 1);
            if (str_sql.Length > 0) str_sql = str_sql + ")";
            if (str_sql == "")
                tb_price.Text = "0.00";
            else
                tb_price.Text = ywszbiz.Get_ZhxmJg(str_sql);
            try
            {
                tb_sale.Text = (Convert.ToDecimal(tb_price.Text) * Convert.ToDecimal(tb_rebate.Text) / 100).ToString("0.00");
            }
            catch
            {

            }
        }

        private void txt_disp_order_Leave(object sender, EventArgs e)
        {
            //txt_disp_order.Text = new Common.Common().CharConverter(txt_disp_order.Text.Trim());
        }
    }
}
